-
Notifications
You must be signed in to change notification settings - Fork 3k
API: Fix equals and hashCode in CharSequenceSet #9245
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
API: Fix equals and hashCode in CharSequenceSet #9245
Conversation
| set2.add(new StringBuffer("v2")); | ||
| set2.add("v3"); | ||
|
|
||
| Set<CharSequence> set3 = Collections.unmodifiableSet(set2); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am primarily interested in wrapping CharSequenceSet into UnmodifiableSet in CharSequenceMap.
f914a5f to
b9b9561
Compare
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) { | ||
| public boolean equals(Object other) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See Set#equals() and AbstractSet#equals() for background.
| return wrapperSet.equals(that.wrapperSet); | ||
| try { | ||
| return containsAll(that); | ||
| } catch (ClassCastException | NullPointerException unused) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While this class does not throw these exceptions now, it may in the future. These exceptions are valid outcomes defined by the Set API. Therefore, I am respecting that to be safe.
| @Override | ||
| public int hashCode() { | ||
| return Objects.hashCode(wrapperSet); | ||
| return wrapperSet.stream().mapToInt(CharSequenceWrapper::hashCode).sum(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Set API requires the hash code to be a sum of hash codes of all elements. See Set#hashCode().
|
|
||
| Set<CharSequence> set3 = Collections.unmodifiableSet(set2); | ||
|
|
||
| Set<CharSequenceWrapper> set4 = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one works too now.
szehon-ho
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good. Another option is to extend java AbstractSet, wondering if we considered it?
|
@szehon-ho, I started by extending |
|
Thanks, @szehon-ho! |

The
equalsandhashCodebehaviors inCharSequenceSetcontradict theSetAPI, which prohibits wrapping these sets into unmodifiable wrappers inCharSequenceMap#keySet.